Deep Learning to find Orchards in Annapolis Royal

In [1]:
#######################################################################################################################
# Script Name: Orchards_Detection_Attempt8.ipynb
# Purpose: To complete the entire object detection workflow in one script
# Modifications to code and comments by: Shannon England
#
# Note: the original script was obtained from the arcgis-python-api-master folder,downloaded from the website
# https://github.com/Esri/arcgis-python-api
#######################################################################################################################
In [2]:
# showing the path to the imagery used to create the training samples
# this was done using the Export Trianing Samples for Deep Learning tool in ArcGIS Pro

mosaic = r'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Rotated_Mosaic.tif' 
mosaic
Out[2]:
'E:\\ShannonE\\GDA_Project_SE\\Historical_Orthomosaic\\Rotated_Mosaic.tif'
In [3]:
# connecting to folder containing exported training samples : settings were 750 x 750 pixels with 375 stride in x and y direction
training_data = r'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Attempt8'
training_data
Out[3]:
'E:\\ShannonE\\GDA_Project_SE\\Historical_Orthomosaic\\Attempt8'

Prepare Data

In [4]:
# importing prepare data library from arcgis to apply transformations and enhance the orchards layer
from arcgis.learn import prepare_data

data = prepare_data(training_data, {1: 'Young Orchard',
                                    2: 'Mixed Orchard',
                                    4: 'Overexposed'})
In [5]:
data.show_batch()

Train SingleShotDetector Model

In [6]:
# importing the object detection model that will be used
from arcgis.learn import SingleShotDetector

# settings for single shot detector
ssd = SingleShotDetector(data, grids=[2,1])
In [7]:
# finding the optimum learning rate - low learning rate leads to slow training of model
ssd.lr_find()
In [8]:
# showing the model learning the task by showing the loss and validation for the training data
ssd.fit(10, slice(0.001, 0.02))
epoch train_loss valid_loss
1 235.774414 754.974915
2 156.127518 237.669861
3 131.243011 101.054451
4 113.051331 85.553215
5 102.591904 79.696640
6 93.387970 75.855988
7 90.120064 66.342606
8 86.701965 72.211411
9 82.396492 69.428154
10 78.237892 71.271477
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpro_clone\lib\site-packages\torch\nn\_reduction.py:49: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.
  warnings.warn(warning.format(ret))
In [13]:
# visualizing the results of the trained model
ssd.show_results(rows=20, thresh=0.05)
In [14]:
# saving the trained model
ssd.save('OrchardDetector9')
---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-14-fe3e91074aa3> in <module>()
      1 # saving the trained model
----> 2 ssd.save('OrchardDetector9')

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpro_clone\lib\site-packages\arcgis\learn\models\_ssd.py in save(self, name_or_path)
    350         with open(saved_path.parent / _EMD_TEMPLATE['InferenceFunction'], 'w') as f:
    351             f.write(code)
--> 352         self._create_zip(zip_name, str(saved_path.parent))
    353         print('Created model files at {spp}'.format(spp=saved_path.parent))
    354 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpro_clone\lib\site-packages\arcgis\learn\models\_ssd.py in _create_zip(self, zipname, path)
    284         import shutil
    285         zip_file = shutil.make_archive(zipname, 'zip', path)
--> 286         shutil.move(zip_file, path)
    287 
    288     def _create_emd(self, path):

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpro_clone\lib\shutil.py in move(src, dst, copy_function)
    540         real_dst = os.path.join(dst, _basename(src))
    541         if os.path.exists(real_dst):
--> 542             raise Error("Destination path '%s' already exists" % real_dst)
    543     try:
    544         os.rename(src, real_dst)

Error: Destination path 'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Attempt8\models\OrchardDetector9\OrchardDetector9.zip' already exists